#include <afxwin.h>
#include "resource.h"
class CMyApp:public CWinApp
{
public:
	BOOL InitInstance();
}theApp;

class Dialog:public CDialog
{
	CButton bt[15][15];
	int BanCo[15][15];
	bool luot1;
	char s[40];
	char nuocdi[20];
	int tongsoluotdi;
public:
	Dialog(UINT ID, CWnd *owner=NULL):CDialog(ID,owner)
	{
		tongsoluotdi = 0;
		luot1 = true;
	}
	BOOL OnInitDialog();
	void OnNewGame();
	void OnBClick(UINT id);
	bool KtraNgang(int id);
	bool KtraDoc(int id);
	bool KtraCheo1(int id);
	bool KtraCheo2(int id);
	DECLARE_MESSAGE_MAP()
};

BOOL CMyApp::InitInstance()
{
	m_pMainWnd=new Dialog(IDD_DIALOG1,NULL);
	int kq=((Dialog*)m_pMainWnd)->DoModal();
	return true;
}

BEGIN_MESSAGE_MAP(Dialog,CDialog)
ON_COMMAND_RANGE(1000,1224,OnBClick)
ON_COMMAND(IDC_BUTTON1,OnNewGame)
END_MESSAGE_MAP()

BOOL Dialog::OnInitDialog()
{
	CRect r;
	int idButton = 1000;
	for(int i = 0; i < 15; i++)
	{
		r.top = i*30;
		r.bottom = i*30+30;
		for(int j = 0; j < 15; j++)
		{
			r.left = j*30;
			r.right = j*30+30;
			bt[i][j].Create("",WS_VISIBLE,r,this,idButton);
			bt[i][j].ShowWindow(SW_SHOW);
			idButton++;
		}
	}
	return true;
}

void Dialog::OnNewGame()
{
	CListBox *lsb;
 	lsb=(CListBox*)GetDlgItem(IDC_LIST1);

	for(int index = 0; index<tongsoluotdi; index++)
		lsb->DeleteString(0);

	CButton *bt;
	int idButton = 1000;
	for(int i = 0; i<15; i++)
	for(int j = 0; j<15; j++)
	{
		bt=(CButton*)GetDlgItem(idButton);
		bt->SetWindowText("");
		BanCo[i][j] = 0;
		idButton++;
	}
	luot1 = true;
	tongsoluotdi = 0;
}

void Dialog::OnBClick(UINT id)
{
	CButton *bt;
	bt=(CButton*)GetDlgItem(id);
	bt->GetWindowText(s,40);
	CListBox *lsb;
	lsb=(CListBox*)GetDlgItem(IDC_LIST1);

	if(strlen(s)!=0)
	{
		AfxMessageBox("Ban da di nuoc co nay roi!");
		return;
	}

	if(luot1 == true)
	{
		bt->SetWindowText("X");
		BanCo[(id-1000)/15][(id-1000)%15] = 1;
		sprintf(nuocdi,"X: hang %d - cot %d",(id-1000)/15+1,(id-1000)%15+1);
		luot1= false;
	}
	else
	{
		bt->SetWindowText("O");
		BanCo[(id-1000)/15][(id-1000)%15] = 2;
		sprintf(nuocdi,"O: hang %d - cot %d",(id-1000)/15+1,(id-1000)%15+1);
		luot1 = true;
	}

	lsb->AddString(nuocdi);
	tongsoluotdi++;

	if(KtraNgang(id) == true || KtraDoc(id) == true || KtraCheo1(id) == true || KtraCheo2(id) == true)
	{
		if(luot1 == true)
			strcpy(s,"Chuc mung O da thang!\nBan co muon choi tiep?");
		else
			strcpy(s,"Chuc mung X da thang!\nBan co muon choi tiep?");
		if(AfxMessageBox(s,MB_YESNO|MB_ICONQUESTION) == IDYES)
			OnNewGame();
		else
			exit(0);
			
	}
}

bool Dialog::KtraNgang(int id)
{
	int dem = 0;
	int hang = (id-1000)/15, cot = (id-1000)%15;
	int i = cot+1;
	while(i<15)
	{
		if(BanCo[hang][i] == BanCo[hang][cot]) dem++;
		else break;
		i++;
	}

	if(dem >=4) return true;

	i = cot-1;
	while(i>=0)
	{
		if(BanCo[hang][i] == BanCo[hang][cot]) dem++;
		else break;
		i--;
	}

	return dem>=4;
}

bool Dialog::KtraDoc(int id)
{
	int dem = 0;
	int hang = (id-1000)/15, cot = (id-1000)%15;

	int i = hang+1;
	while(i<15)
	{
		if(BanCo[i][cot] == BanCo[hang][cot]) dem++;
		else break;
		i++;
	}
	
	if(dem >=4) return true;
	
	i = hang-1;
	while(i>=0)
	{
		if(BanCo[i][cot] == BanCo[hang][cot]) dem++;
		else break;
		i--;
	}
	
	return dem>=4;
}

bool Dialog::KtraCheo1(int id)
{
	int dem = 0;
	int hang = (id-1000)/15, cot = (id-1000)%15;
	
	int i = hang - 1, j = cot + 1;
	while(i<15 && j<15)
	{
		if(BanCo[i][j] == BanCo[hang][cot]) dem++;
		else break;
		i--;
		j++;
	}
	
	if(dem >=4) return true;
	
	i = hang + 1, j = cot - 1;
	while(i>0 && j>0)
	{
		if(BanCo[i][j] == BanCo[hang][cot]) dem++;
		else break;
		i++;
		j--;
	}

	return dem>=4;
}

bool Dialog::KtraCheo2(int id)
{
	int dem = 0;
	int hang = (id-1000)/15, cot = (id-1000)%15;
	
	int i = hang - 1, j = cot - 1;
	while(i>0 && j>0)
	{
		if(BanCo[i][j] == BanCo[hang][cot]) dem++;
		else break;
		i--;
		j--;
	}
	
	if(dem >=4) return true;
	
	i = hang + 1, j = cot + 1;
	while(i<15 && j<15)
	{
		if(BanCo[i][j] == BanCo[hang][cot]) dem++;
		else break;
		i++;
		j++;
	}
	
	return dem>=4;
}